package mainFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.xuegengwang.xuegengwang.R;

import java.util.ArrayList;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import mainFragment.activity.CollegeIndexVolunteerList;
import mainFragment.activity.CollegeMoreDetail;
import mainFragment.adapter.CollegeIndexOAAdapter1;
import mainFragment.bean.CollegeIndexBean;
import mainFragment.bean.CollegeIndexOABean;
import mainFragment.bean.CollegeVolunteerBean;

/**
 * Created by Administrator on 2017/3/3.
 * 在线提问，加上高校的信息
 */
@SuppressLint("ValidFragment")
public class OnLineAsk extends BaseFragment {
    private View view;
    private ListView college_fragment_oa_lv;
    private CollegeIndexOAAdapter1 collegeIndexOAAdapter;
//    private ArrayList<ArrayList<CollegeVolunteerBean>> CollegeVolunteerBeanList;
    private ArrayList<CollegeIndexOABean> arrayList;
    private String id;
//    private ArrayList<List> hashMapsList;
    private Context context;

    private Handler CollegeMoreDetailHandler;

    //TODO
    //v1.3.0删代码
//    private Handler myHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case Constant.ERROR_CODE_SUCCESS:
//                    Gson gson = new Gson();
//                    CollegeIndexBean collegeIndexBean = gson.fromJson((String) msg.obj, CollegeIndexBean.class);
//                    if (collegeIndexBean != null) {
//                        hashMapsList.clear();
//                        List<CollegeIndexBean.VipVolunteerBean> vipVolunteer = collegeIndexBean.getVipVolunteer();
//                        if (vipVolunteer != null) {
//                            hashMapsList.add(vipVolunteer);
//                        }
//                        List<CollegeIndexBean.NormalVolunteer> normalVolunteer = collegeIndexBean.getNormalVolunteer();
//                        if (normalVolunteer != null) {
//                            hashMapsList.add(normalVolunteer);
//                        }
//                        upDateList(hashMapsList);
//                    }
//                    break;
//                default:
//                    break;
//            }
//
//        }
//    };

    public OnLineAsk() {
    }

    public OnLineAsk(Context context, String id) {
        this.id = id;
        this.context = context;
    }

    public OnLineAsk(Context context, String id, Handler handler) {
        this.id = id;
        this.context = context;
        CollegeMoreDetailHandler = handler;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        hashMapsList = new ArrayList<>();
//        CollegeVolunteerBeanList = new ArrayList<>();
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.college_fragment_oa, null, false);
        }
        if (getMyActivity() != null) {
            initView();
        }
        if (CollegeMoreDetailHandler != null) {
            Message message = new Message();
            message.what = CollegeMoreDetail.ONLINEASK_COMPLETE;
            CollegeMoreDetailHandler.sendMessage(message);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return view;
    }

    private void initView() {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.college_fragment_oa, null, false);
        }
        arrayList = new ArrayList<>();
        CollegeIndexOABean collegeIndexOABean = new CollegeIndexOABean(Constant.VOLUNTEER_TYPE_OFFICIAL_NAME,"经该校招生办认证的咨询员，免费为报考学子答疑解惑。");
        CollegeIndexOABean collegeIndexOABean1 = new CollegeIndexOABean(Constant.VOLUNTEER_TYPE_VIP_NAME,"高校内回答经验丰富的学长学姐，了解学校内各个方面的情况，有偿答疑。");
        CollegeIndexOABean collegeIndexOABean2 = new CollegeIndexOABean(Constant.VOLUNTEER_TYPE_NORMAL_NAME,"平台推荐的与您同一生源地甚至同一高中母校的志愿者，免费答疑。");
        arrayList.add(collegeIndexOABean);
        arrayList.add(collegeIndexOABean1);
        arrayList.add(collegeIndexOABean2);
        collegeIndexOAAdapter = new CollegeIndexOAAdapter1(getMyActivity(),arrayList);
        View header = LayoutInflater.from(getMyActivity()).inflate(R.layout.college_listview_header, null);
        college_fragment_oa_lv = (ListView) view.findViewById(R.id.college_fragment_oa_lv);
        college_fragment_oa_lv.addHeaderView(header,null,false);
        college_fragment_oa_lv.setAdapter(collegeIndexOAAdapter);
        college_fragment_oa_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id1) {
                Intent i = new Intent(getMyActivity(), CollegeIndexVolunteerList.class);
                i.putExtra("name",arrayList.get(position-1).getText());
                i.putExtra("college_id",id);
                startActivity(i);
            }
        });
    }

    public void setOnLineAskBean(CollegeIndexBean collegeIndexBean) {
//        if (hashMapsList != null && CollegeVolunteerBeanList != null) {
//            if (collegeIndexBean != null) {
//                hashMapsList.clear();
//                List<CollegeIndexBean.VipVolunteerBean> vipVolunteer = collegeIndexBean.getVipVolunteer();
//                if (vipVolunteer != null) {
//                    hashMapsList.add(vipVolunteer);
//                }
//                List<CollegeIndexBean.NormalVolunteer> normalVolunteer = collegeIndexBean.getNormalVolunteer();
//                if (normalVolunteer != null) {
//                    hashMapsList.add(normalVolunteer);
//                }
//                upDateList(hashMapsList);
//            } else {
//                getDataFromInternet();
//            }
//        }
    }

    private void upDateList(ArrayList<List> hashMapsList) {
//        List<CollegeIndexBean.VipVolunteerBean> vipVolunteer = null;
//        if (hashMapsList.size() > 0) {
//            vipVolunteer = hashMapsList.get(0);
//        }
//        List<CollegeIndexBean.NormalVolunteer> normalVolunteers = null;
//        if (hashMapsList.size() > 1) {
//            normalVolunteers = hashMapsList.get(1);
//        }
//        CollegeVolunteerBeanList.clear();
//        if (vipVolunteer != null) {
//            ArrayList<CollegeVolunteerBean> vip = new ArrayList<>();
//            for (int i = 0; i < vipVolunteer.size(); i++) {
//                CollegeVolunteerBean collegeVolunteerBean = new CollegeVolunteerBean();
//                collegeVolunteerBean.setName(vipVolunteer.get(i).getName());
//                collegeVolunteerBean.setVip(true);
//                collegeVolunteerBean.setId(vipVolunteer.get(i).getId());
//                collegeVolunteerBean.setHeadimg_url(vipVolunteer.get(i).getHeadimgurl());
//                collegeVolunteerBean.setBrief(vipVolunteer.get(i).getBrief());
//                vip.add(collegeVolunteerBean);
//            }
//            CollegeVolunteerBeanList.add(vip);
//        }
//        if (normalVolunteers != null) {
//            ArrayList<CollegeVolunteerBean> normal = new ArrayList<>();
//            for (int i = 0; i < normalVolunteers.size(); i++) {
//                CollegeVolunteerBean collegeVolunteerBean = new CollegeVolunteerBean();
//                collegeVolunteerBean.setName(normalVolunteers.get(i).getName());
//                collegeVolunteerBean.setVip(false);
//                collegeVolunteerBean.setId(normalVolunteers.get(i).getId());
//                collegeVolunteerBean.setHeadimg_url(normalVolunteers.get(i).getHeadimgurl());
//                collegeVolunteerBean.setBrief(normalVolunteers.get(i).getBrief());
//                normal.add(collegeVolunteerBean);
//            }
//            CollegeVolunteerBeanList.add(normal);
//        }
//        collegeIndexOAAdapter.notifyDataSetChanged();
    }

//
//    private void getDataFromInternet() {
//        if (id != null) {
//            HashMap<String, String> hashMap = new HashMap<>();
//            hashMap.put("id", id);
//            new InterNetController(getMyActivity(), Constant.COLLEGEDETAIL, myHandler, hashMap, 0,true);
//        }
//    }


}
